package net.sapfree.display.tags;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.apache.log4j.Logger;


import net.sapfree.common.Constants;
import net.sapfree.core.db.DBConnectionProvider;
import net.sapfree.core.db.DataBaseExecutor;
import net.sapfree.core.db.Record;
import net.sapfree.core.db.RecordSet;
import net.sapfree.display.tags.core.LoopTagSupport;

public class ArticleListTag extends LoopTagSupport {
	private static Logger log = Logger.getLogger(ArticleListTag.class.getClass());
	private RecordSet articleList=null;
	private String filterPersonId;
	private String filterQuanUuid;
	private int count=-1;
	@Override
	protected boolean hasNext() throws JspException {
		return (articleList != null)&&(step<end);
	}

	@Override
	protected Object next() throws JspException {
		
		Record r=articleList.get(step);
		step++;
		return r;
	}

	 
	
	@Override
	protected void prepare() throws JspException {
		ServletRequest request = pageContext.getRequest();
		
		Connection conn =null;
		try {
			conn = DBConnectionProvider.getConnection(Constants.JNDI_SAPFREE);
			DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
			if(filterPersonId!=null&&!filterPersonId.equals("")){
				articleList=dbe.find(getArticleListSql());
			}else if(filterQuanUuid!=null&&!filterQuanUuid.equals("")){
				articleList=dbe.find(getArticleOfQuanListSql());
			}else{
				return;
			}
			
			pageContext.setAttribute("ArticleListTag", this, PageContext.PAGE_SCOPE);
			end=articleList.size();
		 	step=0;
		} catch (SQLException e) {
			log.error("在执查找圈子列表时出错",e);
		}finally{
			try{
				 conn.close();
			}catch(Exception e){}
		}
	}
	
	
	
	private String getArticleListSql(){
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT AI.*,AC.* FROM ARTICLEINFO AI LEFT JOIN ARTICLECONTENT AC ON AI.AI_UUID = AC.AC_ARTICLEUUID  " );
		sql.append(" WHERE AI.AI_PERSONUUID='").append(filterPersonId).append("'");
		sql.append(" ORDER BY AI_PUBLISHTIME DESC");
		if(count!=-1){
			sql.append(" limit 0,").append(count);
		}
		return sql.toString();
	}

	private String getArticleOfQuanListSql(){
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT AI.* FROM ARTICLEQUANRELATION AQR LEFT JOIN " );
		sql.append("(SELECT * FROM ARTICLEINFO LEFT JOIN ARTICLECONTENT ON ARTICLEINFO.AI_UUID=ARTICLECONTENT.AC_UUID)AI");
		sql.append(" ON AQR.AQR_ARTICLEUUID=AI.AI_UUID ");
		sql.append(" WHERE AQR.AQR_QUANUUID='").append(filterQuanUuid).append("'");
		if(count!=-1){
			sql.append(" LIMIT 0,").append(count);
		}
		return sql.toString();
	}
	
	
	/**
	 * @return the filterName
	 */
	public String getFilterPersonId() {
		return filterPersonId;
	}

	/**
	 * @param filterName the filterName to set
	 */
	public void setFilterPersonId(String filterPersonId) {
		this.filterPersonId = filterPersonId;
	}
	
	/**
	 * @return the filterName
	 */
	public String getFilterQuanUuid() {
		return filterQuanUuid;
	}

	/**
	 * @param filterName the filterName to set
	 */
	public void setFilterQuanUuid(String filterQuanUuid) {
		this.filterQuanUuid = filterQuanUuid;
	}

	/**
	 * @return the count
	 */
	public int getCount() {
		return count;
	}

	/**
	 * @param count the count to set
	 */
	public void setCount(int count) {
		this.count = count;
	}



}



